package com.systop.busafter.driver.controller;

import com.systop.commer.Io;
import com.systop.commer.ResponseResult;
import com.systop.commer.pojo.Driver;
import com.systop.busafter.driver.service.DriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
public class DriverController {
    @Autowired
    private DriverService driverService;
    //修改
    @RequestMapping(value="/updatadriver",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult updatadriver(HttpServletRequest request, @RequestParam(value = "driver_certificate",required = false) MultipartFile driver_certificate,
                                @RequestParam(value = "driver_personal",required = false) MultipartFile driver_personal,
                                @RequestParam(value = "driver_name_n")String driver_name_n,@RequestParam(value = "driver_password")String driver_password,
                                @RequestParam(value = "driver_name")String driver_name,@RequestParam(value = "driver_card")String driver_card,
                                @RequestParam(value = "driver_fraction")String driver_fraction,@RequestParam(value = "driver_state")String driver_state,
                                @RequestParam(value = "driver_number")String driver_number,@RequestParam(value = "driver_deposit")String driver_deposit,
                                @RequestParam(value = "driver_remarks")String driver_remarks,@RequestParam(value = "driver_s")String driver_s,
                                @RequestParam(value = "driver_id")String driver_id) throws IOException{
        Driver driver = new Driver();
        driver.setDriver_certificate(Io.img(driver_certificate,request));
        driver.setDriver_personal(Io.img(driver_personal,request));
        driver.setDriver_name_n(driver_name_n);
        driver.setDriver_password(Io.encode(driver_password.getBytes()));
        driver.setDriver_name(driver_name);
        driver.setDriver_card(driver_card);
        driver.setDriver_id(Integer.parseInt(driver_id));
        driver.setDriver_fraction(Integer.parseInt(driver_fraction));
        driver.setDriver_state(Integer.parseInt(driver_state));
        driver.setDriver_number(driver_number);
        driver.setDriver_deposit(Float.parseFloat(driver_deposit));
        driver.setDriver_remarks(driver_remarks);
        driver.setDriver_s(driver_s);
        int i =  driverService.updatadriver(driver);
        if (i == 0) {
            return ResponseResult.build(500, "修改失败");
        } else {
            return ResponseResult.build(200, "修改成功", i);
        }
    }
    //添加
    @RequestMapping(value="/insertdriver",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult insertdriver(HttpServletRequest request, @RequestParam(value = "driver_certificate",required = false) MultipartFile driver_certificate,
                                @RequestParam(value = "driver_personal",required = false) MultipartFile driver_personal,
                                @RequestParam(value = "driver_name_n")String driver_name_n,@RequestParam(value = "driver_password")String driver_password,
                                @RequestParam(value = "driver_name")String driver_name,@RequestParam(value = "driver_card")String driver_card) throws IOException {
        Driver driver = new Driver();
        driver.setDriver_certificate(Io.img(driver_certificate,request));
        driver.setDriver_personal(Io.img(driver_personal,request));
        driver.setDriver_name_n(driver_name_n);
        driver.setDriver_password(Io.encode(driver_password.getBytes()));
        driver.setDriver_name(driver_name);
        driver.setDriver_card(driver_card);
        int i =  driverService.insertdriver(driver);
        if (i == 0) {
            return ResponseResult.build(500, "添加失败");
        } else {
            return ResponseResult.build(200, "添加成功", i);
        }
    }
    //账号密码登陆
    @RequestMapping(value="/driverLogin",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult driverLogin(@RequestBody Driver driver){
        String encode = Io.encode(driver.getDriver_password().getBytes());
        driver.setDriver_password(encode);
        Driver driver1  =  driverService.driverLogin(driver);
        if (driver1 == null) {
            return ResponseResult.build(500, "登陆失败");
        } else {
            return ResponseResult.build(200, "登陆成功", driver1);
        }
    }
    //手机号密码登陆
    @RequestMapping(value="/numdriverLogin",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult numdriverLogin(@RequestBody Driver driver){
        Driver driver1  =  driverService.numdriverLogin(driver);
        if (driver1 == null) {
            return ResponseResult.build(500, "登陆失败");
        } else {
            return ResponseResult.build(200, "登陆成功", driver1);
        }
    }

    //查看全部
    @RequestMapping(value="/driverListall",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult driverListall(){
        List<Driver> list =  driverService.driverListall();
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    //通过id查
    @RequestMapping(value="/driverIdlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult driverIdlist(@RequestParam(value = "driver_id")int driver_id){
        List<Driver> list =  driverService.driverIdlist(driver_id);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    //删除
    @RequestMapping(value="/deletedriver",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult deletedriver(@RequestParam(value = "driver_id")int driver_id){
       int i =  driverService.deletedriver(driver_id);
        if (i == 0) {
            return ResponseResult.build(500, "删除失败");
        } else {
            return ResponseResult.build(200, "删除成功", i);
        }
    }

    //通过分数查
    @RequestMapping(value="/driverFractionlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult driverFractionlist(@RequestParam(value = "driver_fraction")int driver_fraction){
        List<Driver> list =  driverService.driverFractionlist(driver_fraction);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    //通过状态查
    @RequestMapping(value="/driverStatelist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult driverStatelist(){
        int driver_state=1;
        List<Driver> list =  driverService.driverStatelist(driver_state);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    //通过押金查
    @RequestMapping(value="/driverDepositlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult driverDepositlist(@RequestParam(value = "driver_deposit")int driver_deposit){
        List<Driver> list =  driverService.driverDepositlist(driver_deposit);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    //通过名字查
    @RequestMapping(value="/driverNamelist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult driverNamelist(@RequestParam(value = "driver_name")String driver_name){
        List<Driver> list =  driverService.driverNamelist(driver_name);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }


    //验证账号
    @RequestMapping(value="/drivername",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult drivername(@RequestParam(value = "driver_name_n")String driver_name_n){
        List<Driver> list = driverService.drivername(driver_name_n);
        if (list == null) {
            return ResponseResult.build(200, "");
        } else {
            return ResponseResult.build(500, "账号或身份证号重复1", list);
        }
    }


    //验证身份证号
    @RequestMapping(value="/drivercard",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult drivercard(@RequestParam(value = "driver_card")String driver_card){
        List<Driver> list = driverService.drivercard(driver_card);

        if (list == null) {
            return ResponseResult.build(500, "");
        } else {
            return ResponseResult.build(200, "账号或身份证号重复1", list);
        }
    }

    //审核状态driveridtureT
    @RequestMapping(value="/driveridtureT",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult drivercard(@RequestParam(value = "driver_id")Integer driver_id){
        List<Driver> list = driverService.driverIdlist(driver_id);
        Driver driver = new Driver();
        driver.setDriver_password(list.get(0).getDriver_password());
        driver.setDriver_name_n(list.get(0).getDriver_name_n());
        Driver driver1 = driverService.driverLogin(driver);
        if (list.get(0).getDriver_state() == 0 ){
            driver1.setDriver_state(1);
        }else{
            driver1.setDriver_state(0);
        }
        int i = driverService.updatadriver(driver1);
        if (i == 0) {
            return ResponseResult.build(500, "");
        } else {
            return ResponseResult.build(200, "审核状态成功", i);
        }
    }
}
